package com.waiter.moximoxi.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.WindowManager;

import com.landicorp.android.eptapi.DeviceService;
import com.landicorp.android.eptapi.exception.ReloginException;
import com.landicorp.android.eptapi.exception.RequestException;
import com.landicorp.android.eptapi.exception.ServiceOccupiedException;
import com.landicorp.android.eptapi.exception.UnsupportMultiProcess;
import com.waiter.moximoxi.R;
import com.waiter.moximoxi.net.API;
import com.waiter.moximoxi.ui.LoginActivity;
import com.waiter.moximoxi.utils.ActivityCollector;

import butterknife.ButterKnife;
import cn.yhong.aframework.activity.AfActivity;
import cn.yhong.aframework.presenter.BasePresenter;
import cn.yhong.aframework.view.ILoginView;


public abstract class BaseActivity<P extends BasePresenter> extends AfActivity<P> implements ILoginView {

    /**
     * 当前Activity是否可见
     */
    public static boolean isForeground = false;

    private boolean isDeviceServiceLogined = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置底部导航栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.colorWhite));
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }

        ActivityCollector.addActivity(this);
    }


    @Override
    protected void onResume() {
        super.onResume();

        isForeground = true;//位于前台
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);

        if (mActivity != null) {
            mActivity = null;
        }
    }


    @Override
    protected void onPause() {
        isForeground = false;//位于后台
        super.onPause();
    }


    @Override
    public boolean isLogin() {
        return API.isLogin();
    }

    @Override
    public void toLoginActivity() {
        startActivityForResult(new Intent(mActivity, LoginActivity.class).putExtra("isfirst", "1"), 998);
    }

    @Override
    public void toLoginSuccess(Intent data) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 998 && resultCode == -1 && data != null) {
            toLoginSuccess(data);
        }
    }

    public void bindButterKnife() {
        // 绑定注解
        ButterKnife.bind(this);
    }

    public void unBindButterKnife() {
        // 解绑注解
        ButterKnife.unbind(this);
    }


    /**
     * To gain control of the device service,
     * you need invoke this method before any device operation.
     */
    public void bindDeviceService() {
        try {
            isDeviceServiceLogined = false;
            DeviceService.login(this);
            isDeviceServiceLogined = true;
        } catch (RequestException e) {
            // Rebind after a few milliseconds,
            // If you want this application keep the right of the device service
//			runOnUiThreadDelayed(new Runnable() {
//				@Override
//				public void run() {
//					bindDeviceService();
//				}
//			}, 300);
            e.printStackTrace();
        } catch (ServiceOccupiedException e) {
            e.printStackTrace();
        } catch (ReloginException e) {
            e.printStackTrace();
        } catch (UnsupportMultiProcess e) {
            e.printStackTrace();
        }
    }

    /**
     * Release the right of using the device.
     */
    public void unbindDeviceService() {
        DeviceService.logout();
        isDeviceServiceLogined = false;
    }

}
